﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;
    using System.Runtime.InteropServices;

    public partial class frmCapNhatDmKho : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmKhoaEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmKhoaBusiness dmKhoEntityaBuss = new DmKhoaBusiness();
        DmKhoBusiness dmKhoBusiness = new DmKhoBusiness();
        DmKhoEntity dmKhoEntity = null;

        public frmCapNhatDmKho(int state)
        {
            InitializeComponent();
            this.State = state;
            LoadKhoa();
        }

        public frmCapNhatDmKho(int state, DmKhoEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmKhoEntity = obj;
            LoadKhoa();

            txtMaKho.Text = dmKhoEntity.MaKho;
            txtTenKho.Text = dmKhoEntity.TenKho;
            txtDiaChi.Text = dmKhoEntity.DiaChi;
            txtDienThoai.Text = dmKhoEntity.DienThoai;
            lkDmKhoa.EditValue = dmKhoEntity.MaKhoa;
            txtMoTa.Text = dmKhoEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmKhoEntity.TrangThaiSuDung;
            txtMaKho.Enabled = false;
            txtTenKho.Focus();
        }

        void LoadKhoa()
        {
            var lstDmKhoaEntity = dmKhoEntityaBuss.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember,TenKhoa = ConstantManagerClinic.sDisplayMember});
            lkDmKhoa.Properties.DataSource = lstDmKhoaEntity;
            lkDmKhoa.Properties.DisplayMember = "TenKhoa";
            lkDmKhoa.Properties.ValueMember = "MaKhoa";
            lkDmKhoa.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDMKhoa(object sender, EventArgs e)
        {
            LoadKhoa();
        }

        private void frmcapnhatdmkho_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmddmkhoa_Click(object sender, EventArgs e)
        {
            frmCapNhatDmKhoa frm = new frmCapNhatDmKhoa((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmKhoaEvent += new EventHandler(LoadDMKhoa);
            frm.ShowDialog();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkDmKhoa, "Khoa") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaKho, "Mã kho") == false) return;
            
            dmKhoEntity = new DmKhoEntity()
            {
                MaKho = txtMaKho.Text,
                TenKho = txtTenKho.Text.Trim(),
                DiaChi = txtDiaChi.Text.Trim(),
                DienThoai = txtDienThoai.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked,
                MaKhoa = lkDmKhoa.EditValue.ToString()
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmKhoBusiness.GetDmKho(txtMaKho.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaKho, "Mã kho") == false) return;

                dmKhoBusiness.InsertDmKho(dmKhoEntity);
                Validation.ClearControl(this);
                lkDmKhoa.Focus();
            }
            else
            {
                dmKhoBusiness.EditDmKho(dmKhoEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmKhoaEvent != null)
                dmKhoaEvent(sender, e);
        }

    }
}